import * as winston from "winston";
import DailyRotateFile = require("winston-daily-rotate-file");

const format = winston.format;

type LoggerType = "info" | "error";

const customFormat = format.combine(
  format.colorize(),
  format.timestamp({
    format: "YYYY/MM/DD HH:mm:ss",
  }),

  format.splat(),
  format.align(),
  format.printf((info) => {
    return `[${info.timestamp}] : ${info.message}`;
  }),
);

class _ILogger {
  private infoLogger: winston.Logger | undefined = undefined;
  private errorLogger: winston.Logger | undefined = undefined;

  bootstrap(fileDir?: string) {
    this.infoLogger = this.createLogger("info", fileDir);
    this.errorLogger = this.createLogger("error", fileDir);
  }

  createLogger(loggerType: LoggerType, fileDir?: string): winston.Logger {
    const transports: winston.transport[] = [
      new winston.transports.Console({
        level: "info",
      }),
    ];
    transports.push(
      new DailyRotateFile({
        filename: `${fileDir ?? "logs"}/${loggerType}_%DATE%.log`,
        datePattern: "YYYY-MM-DD",
        zippedArchive: true,
        maxSize: "20m",
        maxFiles: "14d",
      }),
    );
    return winston.createLogger({
      exitOnError: false,
      format: customFormat,
      transports,
    });
  }

  info(message: string): void {
    this.infoLogger?.info(message);
  }

  error(message: string): void {
    this.errorLogger?.error(message);
  }
}

const ILogger = new _ILogger();

export default ILogger;
